using System.IO;
using System.Reflection;
using GrapeCity.Documents.Excel;
using System.Collections.Generic;

namespace GrapeCity.Documents.Excel.Examples.Templates.DataSource.Variable
{
    class Program
    {
        static void Main(string[] args)
        {
			//create a new workbook
			var workbook = new GrapeCity.Documents.Excel.Workbook();
			
			//Load template file Template_StudentInfo.xlsx from resource
			var templateFile = GetResourceStream("xlsx\\Template_StudentInfo.xlsx");
			workbook.Open(templateFile);
			
			#region Define custom classes
			//public class StudentInfo
			//{
			//    public string name;
			//    public string address;
			//    public List<Family> family;
			//}
			#endregion
			
			#region Init Data
			var studentInfos = new List<StudentInfo>
			{
			    new StudentInfo
			    {
			        name = "Jane",
			        address = "101, Halford Avenue, Fremont, CA"
			    },
			    new StudentInfo
			    {
			        name = "Mark",
			        address = "2005 Klamath Ave APT, Santa Clara, CA"
			    },
			    new StudentInfo
			    {
			        name = "Carol",
			        address = "1063 E EI Camino Real, Sunnyvale, CA 94087, USA"
			    },
			    new StudentInfo
			    {
			        name = "Liano",
			        address = "1977 St Lawrence Dr, Santa Clara, CA 95051, USA"
			    },
			    new StudentInfo
			    {
			        name = "Hellen",
			        address = "3661 Peacock Ct, Santa Clara, CA 95051, USA"
			    }
			};
			
			var className = "Class 3"; 
			#endregion
			
			//Add data source
			workbook.AddDataSource("className", className);
			workbook.AddDataSource("s", studentInfos);
			
			//Invoke to process the template
			workbook.ProcessTemplate();
			        
			// Save to an excel file
			workbook.Save("Variable.xlsx");

        }

		static Stream GetResourceStream(string resourcePath)
        {
            string resource = "Variable.Resource." + resourcePath.Replace("\\", ".");
            var assembly = typeof(Program).GetTypeInfo().Assembly;
            return assembly.GetManifestResourceStream(resource);
        }

    }
}